/*******************************************************************************



*******************************************************************************/


gl out "${output}/forPaper/FigS4"

cap program drop coefsv
*qui do "${dropbox}/99.Common/coefsv.do"

gl grset graphregion(color(white)) ylabel(, angle(0)) plotregion(margin(sides))

loc log = 1
foreach ind in _nw _fw _icw {
	foreach t in  wk fnt month  { //    
		loc s _`t'
		loc time `t'_dy

		loc lt
		if `log' loc lt _log
		
		loc est = 1
		if `est' {
			use  "${output}\clean\stringency_cases_clean.dta", clear
			if `log' replace case_count`s' = asinh(case_count`s')
			
			*-------------------------------------------------------------------------------
			*		0. General cleaning and cleaning for long panels
			*-------------------------------------------------------------------------------
			*Generate  mcat for long panel data
			gen moy = month(date)
			recode moy (1 2 = 1 "Jan-Feb") (3 4 = 2 "Mar-April") (5 6 = 3 "May-June") ///
				(7 8 = 4 "July-Aug") (9 10 = 5 "Sept-Oct") (11 12 = 6 "Nov-Dec"), gen(mcat)
			gen year = date/365

			drop if ctrytext == "kenyaipush" & date > mdy(10,8,2020)
			drop if ctrytext == "kenyaipush" & round == 0
			drop if ctrytext == "kenyage" & month == 52

			*-------------------------------------------------------------------------------
			*		1.	Add controls for seasonality samples
			*-------------------------------------------------------------------------------

			*				1.1 Kenya GE seasonality control
			*-------------------------------------------------------------------------------
			preserve
			do "${code}/Data Preparation/dataprep_kenya_ge"
			*keep if ctrytext == "kenyage"
			merge m:1 date using "${raw}/kenya_ge/kenya_ge_seas", nogen keep(1 3)
			gen ctrytext = "kenyage"
			tempfile kenyage
			save `kenyage' , replace
			restore

			replace _ctrl_seas = . if ctrytext == "kenyage"
			merge m:1 pid date ctrytext using `kenyage', nogen update keep(1 3 4 5)
			codebook _ctrl_seas if ctrytext == "kenyage"

			*				1.2 Aggregating seasonality controls
			*-------------------------------------------------------------------------------
			egen _mctrl_seas = mean(_ctrl_seas), by(`time' ctry)
			gen __ctrl_seas = _ctrl_seas if date < mdy(3,1,2020)
			replace __ctrl_seas = _mctrl_seas if date > mdy(3,1,2020)
			replace __ctrl_seas = -1*__ctrl_seas
			label var __ctrl_seas "Seasonal Food Security"


			*				1.3 Linear time control __year for kenyaipush
			*-------------------------------------------------------------------------------
			gen __year = date/365
			egen myear = mean(__year), by(`time' ctry)
			replace __year = myear if date > mdy(3,1,2021)


			*-------------------------------------------------------------------------------
			*		2		Make residualized variables, country-specific estimation results
			*-------------------------------------------------------------------------------

			*			2.1		Empty variables to fill
			*-------------------------------------------------------------------------------
			*Residualized vars
			gen strind`s'_res = . 
			gen case_count`s'_res = .
			gen ___depression_res = .

			*Point estimates and SEs for country specific estimation results
			gen sipe = .
			gen ccpe = .
			gen sise = .
			gen ccse = .
			gen plot_ctry = ""


			*		2.2		Loop over all countries and generate residualzed vars
			*-------------------------------------------------------------------------------
			gl ctrys kenyaklps kenyaipush  rwanda colombia  nepal kenyage
			*if "`t'" == "wk" gl ctrys $ctrys kenyage

			loc i = 0
			 foreach c in $ctrys {
				loc ++ i
				loc ctrl year i.mcat
				if regexm("`c'", "(nigeria|bangladesh|sierraleone)") loc ctrl 
				if regexm("`c'", "(nepal|kenyage)") loc ctrl __ctrl_seas
				if "`c'" == "kenyaipush" loc ctrl __year
				foreach v of varlist strind`s' case_count`s'  {
					areg `v' `ctrl' if ctrytext == "`c'", absorb(pid) vce(cluster hhid)
					predict `v'_res_`c' if ctrytext == "`c'", res 
				}
				areg ___depression`ind' `ctrl' if ctrytext == "`c'" , absorb(pid) vce(cluster hhid)
				predict ___depression_res_`c' if ctrytext == "`c'", res

				replace strind`s'_res = strind`s'_res_`c' if ctrytext == "`c'"
				replace case_count`s'_res = case_count`s'_res_`c' if ctrytext == "`c'"
				replace ___depression_res = ___depression_res_`c' if ctrytext == "`c'"
				noi di "----------------------------------------------"
				noi di "                 `c'                        "
				noi di "----------------------------------------------"
				noi eststo `c'`s'fe: areg ___depression`ind' `ctrl' strind`s' case_count`s' if ///
					ctrytext == "`c'", absorb(pid) vce(cluster hhid)
				noi eststo `c'`s'res: reg ___depression_res_`c' strind`s'_res_`c' case_count`s'_res_`c' ///
					if ctrytext == "`c'", cluster(month)
				
				replace plot_ctry = "`c'" if _n == `i'
				cap replace sipe = _b[strind`s'_res_`c'] if _n == `i'
				cap replace sise = _se[strind`s'_res_`c'] if _n == `i'
				cap replace ccpe = _b[case_count`s'_res_`c'] if _n == `i'
				cap replace ccse = _se[case_count`s'_res_`c'] if _n == `i'
			}

			
			*-------------------------------------------------------------------------------
			*		3		Estimate single model, plot all results
			*-------------------------------------------------------------------------------
			clonevar ctry_clust = ctry
			*replace ctry_clust = 2 if inlist(ctry_clust, 10,11)
			label var strind`s'_res "Stringency Index"
			label var case_count`s'_res "Case Counts"
			eststo agg`s': mixed ___depression_res strind`s'_res case_count`s'_res ///
				||ctry: strind`s'_res case_count`s'_res, vce(cluster ctry_clust) 

			loc ++ i 
			replace plot_ctry = "Random Effects Aggregate" if _n == `i'
			replace sipe = _b[strind`s'_res] if _n == `i'
			replace sise = _se[strind`s'_res] if _n == `i'
			replace ccpe = _b[case_count`s'_res] if _n == `i'
			replace ccse = _se[case_count`s'_res] if _n == `i'

			save "${output}\clean\string_case`s'_resid`lt'", replace
		}

		use "${output}\clean\string_case`s'_resid`lt'", clear

		sum strind`s'
		replace sipe = sipe*`r(sd)'
		replace sise = sise*`r(sd)'
		*replace ccpe = ccpe*100
		*replace ccse = ccse*100
		egen sd_cc = sd(case_count`s'), by(country)
		replace ccpe = ccpe*sd_cc
		replace ccse = ccse*sd_cc

		keep plot_ctry sipe sise ccpe ccse
		ren (sipe sise ccpe ccse) (pc2 pcse2 pc3 pcse3)
		keep if !mi(plot_ctry)
		reshape long pc pcse, i(plot_ctry) j(IndepVar)

		
		gen pc_hi = pc+ 1.96*pcse
		gen pc_lo = pc- 1.96*pcse

		replace pc = pc/abs(pc)*0.25 if abs(pc) > .25
		replace pc_hi = pc_hi/abs(pc_hi)*0.25 if abs(pc_hi) > .25
		replace pc_lo = pc_lo/abs(pc_lo)*0.25 if abs(pc_lo) > .25

		*drop if plot_ctry == "bangladesh"
		loc caselbl Case Count per Million
		if `log' loc caselbl IHS(Case Count per Million)
		label define period 2 "Stringency Index" 3 "`caselbl'"

		label values IndepVar vars
		label var IndepVar "Independent Variable"
		label var pc "Effect of Variable on Mental Health"
		
		/*tw (scatter pc IndepVar if pc != 0) (rcap pc_hi pc_lo IndepVar if pc != 0) , ///
			yline(0, lpattern(dash)) ytitle(Effect of 1 SD Change) ///
			xlabel(1 "" 2 "Stringency Index" 3 "`caselbl'" 4 "", angle(20)) ///
			by(plot_ctry, rows(2)  ///
				legend(off) title(Time variable: `t') ) //	
		graph export "${output}/forPaper/strind_case_agg`s'`lt'.png", replace*/
		
		
		keep if plot_ctry == "Random Effects Aggregate"
		if "`t'" == "wk" loc tv Week
		if "`t'" == "fnt" loc tv Fortnight
		if "`t'" == "month" loc tv Month
		tw (scatter pc IndepVar if pc != 0, color(gs0)) (rcap pc_hi pc_lo IndepVar if pc != 0 , color(gs0)) , ///
			yline(0, lpattern(dash)) ytitle(Effect of 1 SD Change) ///
			xlabel(1 "" 2 "Stringency Index" 3 "`caselbl'" 4 "", angle(20)) ///
			 $grset legend(off) //title(Independent Variables Aggregated by `tv')
			graph export "${out}/strind_case_agg`s'`lt'_aggonly`ind'.png", replace
	}
}






